﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Globalization;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;
using _WebShared.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_EmployeePersonalEdit : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        IMasNamePrefixBLL prefixBLL = new MasNamePrefixBLL();
        IMasGenderBLL genderBLL = new MasGenderBLL();
        IMasNationalitiesBLL natBLL = new MasNationalitiesBLL();
        IMasReligionBLL religionBLL = new MasReligionBLL();

        IEmployeeBLL empBLL = new EmployeeBLL();
       // I_HRM_PS_EmployeeParents parent = new B_HRM_PS_EmployeeParents();
        IHisSpouseBLL spouBLL = new HisSpouseBLL();
        IChildrenBLL chilBLL = new ChildrenBLL();
       // I_HRM_PS_EmployeeContacts cont = new B_HRM_PS_EmployeeContacts();
        //I_HRM_PS_EmployeeNameHistories name = new B_HRM_PS_EmployeeNameHistories();
        Main main = new Main();

        private EmployeeInfo empInfo;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDDLGenderSeq();
                LoadDDLNamePrefixSeq();
                LoadDDLRaceNatSeq();
                LoadDDLReligionSeq();
                LoadHeader();
                //LoadPanelBar();
            }

            //(RadGrid)RadPanelBar1.Items[0].Items[0].FindControl("RadGridParentHistory")
        }

        protected void LoadHeader()
        {
            string strEmpSeq = ssMng.EmpSeq;

            if (strEmpSeq.Length == 0)
                return;

            IList<EmployeeInfo> empList = empBLL.ListEmployees(strEmpSeq);

            if (empList.Count > 0)
            {
                empInfo = empList[0];
                lblName.Text = empInfo.Person.FirstName + " " + empInfo.Person.LastName;
                txtFirstName.Text = empInfo.Person.FirstName;
                txtLastName.Text = empInfo.Person.LastName;
                txtFirstNameEng.Text = empInfo.Person.FirstNameEng;
                txtLastNameEng.Text = empInfo.Person.LastNameEng;
                txtBloodGroup.Text = empInfo.Person.BloodGroup.Name;
                txtBirthDate.Text = empInfo.Person.BirthDate.ToString();
                txtCitizen.Text = empInfo.Person.Citizen;
                txtTaxPayer.Text = empInfo.Person.TaxPayer;
                txtSocial.Text = empInfo.Person.Social;

                ddlGenderSeq.SelectedValue = empInfo.Person.Gender.Sequence.ToString();
                LoadDDLNamePrefixSeq();
                ddlNamePrefixSeq.SelectedValue = empInfo.Person.NamePrefix.Sequence.ToString();
                ddlRaceNatSeq.SelectedValue = empInfo.Person.RaceNationalities.Sequence.ToString();
                ddlNatSeq.SelectedValue = empInfo.Person.Nationalities.Sequence.ToString();
                ddlReligionSeq.SelectedValue = empInfo.Person.Religion.Sequence.ToString();

                if (empInfo.Person.BirthDate.HasValue)
                {
                    DateTime dt = DateTime.Parse(empInfo.Person.BirthDate.ToString());
                    lblAgeYear.Text = main.CalculateAge(dt).ToString();
                    lblAgeMonth.Text = main.CalculateAgeMonth(dt).ToString();
                    lblAgeDay.Text = main.CalculateAgeDay(dt).ToString();
                }
                else
                {
                    lblAgeYear.Text = "-";
                    lblAgeMonth.Text = "-";
                    lblAgeDay.Text = "-";
                }
            }

            //DataTable dtb = emp.SelectPersonalDtls(strEmpSeq);

            //if (dtb.Rows.Count > 0)
            //{

    //            lblName.Text = empInfo.EmpName; Misc.StringValue(dtb.Rows[0]["prefix_name"])
    //+ Misc.StringValue(dtb.Rows[0]["first_name"]) + " "
    //+ Misc.StringValue(dtb.Rows[0]["last_name"]);
    //            txtFirstName.Text = Misc.StringValue(dtb.Rows[0]["first_name"]);
    //            txtLastName.Text = Misc.StringValue(dtb.Rows[0]["last_name"]);
    //            txtFirstNameEng.Text = Misc.StringValue(dtb.Rows[0]["first_name_eng"]);
    //            txtLastNameEng.Text = Misc.StringValue(dtb.Rows[0]["last_name_eng"]);
    //            txtBloodGroup.Text = Misc.StringValue(dtb.Rows[0]["blood_group"]);
    //            txtBirthDate.Text = Misc.StringValue(dtb.Rows[0]["birth_date"]);
    //            txtCitizen.Text = Misc.StringValue(dtb.Rows[0]["citizen"]);
    //            txtTaxPayer.Text = Misc.StringValue(dtb.Rows[0]["tax_payer"]);
    //            txtSocial.Text = Misc.StringValue(dtb.Rows[0]["social"]);

    //            ddlGenderSeq.SelectedValue = Misc.StringValue(dtb.Rows[0]["gender_seq"]);
    //            LoadDDLNamePrefixSeq();
    //            ddlNamePrefixSeq.SelectedValue = Misc.StringValue(dtb.Rows[0]["name_prefix_seq"]);
    //            ddlRaceNatSeq.SelectedValue = Misc.StringValue(dtb.Rows[0]["race_nat_seq"]);
    //            ddlNatSeq.SelectedValue = Misc.StringValue(dtb.Rows[0]["nat_seq"]);
    //            ddlReligionSeq.SelectedValue = Misc.StringValue(dtb.Rows[0]["religion_seq"]);
            //}

            //if (Misc.StringValue(dtb.Rows[0]["birth_date"]).Length > 0)
            //{
            //    DateTime dt = (DateTime)DateTimeX.ForceToDate(Misc.StringValue(dtb.Rows[0]["birth_date"]), true);
            //    lblAgeYear.Text = main.CalculateAge(dt).ToString();
            //    lblAgeMonth.Text = main.CalculateAgeMonth(dt).ToString();
            //    lblAgeDay.Text = main.CalculateAgeDay(dt).ToString();
            //}
            //else
            //{
            //    lblAgeYear.Text = "-";
            //    lblAgeMonth.Text = "-";
            //    lblAgeDay.Text = "-";
            //}
        }

        protected void LoadDDLGenderSeq()
        {
            //ddlGenderSeq.DataSource = gen.SelectDropDownList();
            //ddlGenderSeq.DataBind();
        }

        protected void LoadDDLNamePrefixSeq()
        {
            //IList<MasNamePrefixInfo> infoList = prefixBLL.ListNamePrefixes(null, (Nullable<int>) Int32.Parse(ddlGenderSeq.SelectedValue)); 

            //ddlNamePrefixSeq.DataSource = infoList;
            //ddlNamePrefixSeq.DataBind();
        }

        protected void LoadDDLRaceNatSeq()
        {
            IList<MasNationalitiesInfo> infoList = natBLL.ListNationalities();
            ddlRaceNatSeq.DataSource = infoList;
            ddlRaceNatSeq.DataBind();

            ddlNatSeq.DataSource = infoList;
            ddlNatSeq.DataBind();
        }

        protected void LoadDDLReligionSeq()
        {
            IList<MasReligionInfo> infoList = religionBLL.ListReligions();

            ddlReligionSeq.DataSource = infoList;
            ddlReligionSeq.DataBind();
        }

        protected void LoadPanelBar()
        {
            string strEmpSeq = ssMng.EmpSeq;
            RadGrid grd1 = null;

            for (int i = 0; i < RadPanelBar1.Items.Count; i++)
            {
                if (RadPanelBar1.Items[i].Expanded)
                {
                    switch (i)
                    {
                        case 0:
                            grd1 = (RadGrid)RadPanelBar1.Items[0].Items[0].FindControl("RadGridParentHistory");
                            //grd1.DataSource = parent.SelectEmployeeParents(strEmpSeq);
                            grd1.DataBind();
                            break;
                        case 1:
                            grd1 = (RadGrid)RadPanelBar1.Items[1].Items[0].FindControl("RadGridSpouseHistory");
                            //grd1.DataSource = spouse.SelectEmployeeSpouses(strEmpSeq);
                            grd1.DataBind();
                            break;
                        case 2:
                            grd1 = (RadGrid)RadPanelBar1.Items[2].Items[0].FindControl("RadGridChildrenHistory");
                            //grd1.DataSource = child.LoadGridEmployeeChildrens(strEmpSeq);
                            grd1.DataBind();
                            break;
                        case 3:
                            grd1 = (RadGrid)RadPanelBar1.Items[3].Items[0].FindControl("RadGridContactHistory");
                            //grd1.DataSource = cont.SelectEmployeeContacts(strEmpSeq);
                            grd1.DataBind();
                            break;
                        case 4:
                            grd1 = (RadGrid)RadPanelBar1.Items[4].Items[0].FindControl("RadGridNameHistory");
                            //grd1.DataSource = name.SelectEmployeeNameHistories(strEmpSeq);
                            grd1.DataBind();
                            break;
                    }
                }
            }
        }

        protected void RadPanelBar1_ItemClick(object sender, RadPanelBarEventArgs e)
        {
            string strEmpSeq = ssMng.EmpSeq;
            RadGrid grd1 = null;

            if (e.Item.Expanded)
            {
                switch (e.Item.Index)
                {
                    case 0:
                        grd1 = (RadGrid)RadPanelBar1.Items[0].Items[0].FindControl("RadGridParentHistory");
                        //grd1.DataSource = parent.SelectEmployeeParents(strEmpSeq);
                        grd1.DataBind();
                        break;
                    case 1:
                        grd1 = (RadGrid)RadPanelBar1.Items[1].Items[0].FindControl("RadGridSpouseHistory");
                        //grd1.DataSource = spouse.SelectEmployeeSpouses(strEmpSeq);
                        grd1.DataBind();
                        break;
                    case 2:
                        grd1 = (RadGrid)RadPanelBar1.Items[2].Items[0].FindControl("RadGridChildrenHistory");
                        //grd1.DataSource = child.LoadGridEmployeeChildrens(strEmpSeq);
                        grd1.DataBind();
                        break;
                    case 3:
                        grd1 = (RadGrid)RadPanelBar1.Items[3].Items[0].FindControl("RadGridContactHistory");
                        //grd1.DataSource = cont.SelectEmployeeContacts(strEmpSeq);
                        grd1.DataBind();
                        break;
                    case 4:
                        grd1 = (RadGrid)RadPanelBar1.Items[4].Items[0].FindControl("RadGridNameHistory");
                        //grd1.DataSource = name.SelectEmployeeNameHistories(strEmpSeq);
                        grd1.DataBind();
                        break;
                }
            }
        }

        protected void RadGrid_ItemCommand(object source, GridCommandEventArgs e)
        {
            RadGrid grd1 = (RadGrid)source;
            string strEmpSeq = ssMng.EmpSeq;
            
            switch (grd1.ID)
            {
                case "RadGridParentHistory":
                    //grd1.DataSource = parent.SelectEmployeeParents(strEmpSeq);
                    break;
                case "RadGridSpouseHistory":
                    //grd1.DataSource = spouse.SelectEmployeeSpouses(strEmpSeq);
                    break;
                case "RadGridChildrenHistory":
                    //grd1.DataSource = child.LoadGridEmployeeChildrens(strEmpSeq);
                    break;
                case "RadGridContactHistory":
                    //grd1.DataSource = cont.SelectEmployeeContacts(strEmpSeq);
                    break;
                case "RadGridNameHistory":
                    //grd1.DataSource = name.SelectEmployeeNameHistories(strEmpSeq);
                    break;
            }

            if (e.CommandName != RadGrid.ExpandCollapseCommandName)
            {
                grd1.DataBind();
            }
        }

        public string GetTrueFalseImage(string strCompare, string strTrue, string strFalse
            , string strTrueImageUrl, string strFalseImageUrl)
        {
            if (strCompare == strTrue)
                return strTrueImageUrl;
            else if (strCompare == strFalse)
                return strFalseImageUrl;
            else
                return null;
        }

        protected void CheckDateValid(object source, ServerValidateEventArgs args)
        {
            if (args.Value.Length > 0)
            {
                if (!DateTimeX.IsValidDmyDate(args.Value, true))
                {
                    args.IsValid = false;
                }
            }
            else
            {
                args.IsValid = true;
            }
        }

        protected void CheckCitizen(object source, ServerValidateEventArgs args)
        {
            //if (!DateTimeX.IsValidDmyDate(args.Value, true))
            //{
            //    args.IsValid = false;
            //}
        }

        protected void CheckBloodGroup(object source, ServerValidateEventArgs args)
        {
            if (args.Value.Length > 0)
            {
                switch (args.Value.Trim().ToUpper())
                {
                    case "A" :
                    case "B" :
                    case "AB" :
                    case "O" :
                        args.IsValid = true;
                        break;
                    default :
                        args.IsValid = false;
                        break;
                }
            }
            else
            {
                args.IsValid = true;
            }
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            //empInfo = new EmployeeInfo();

            //empInfo.Seq = ssMng.EmpSeq.Length > 0 ? ssMng.EmpSeq : (int?)null;
            //empInfo.NamePrefixSeq = ddlNamePrefixSeq.SelectedValue.Length > 0 ? Int32.Parse(ddlNamePrefixSeq.SelectedValue) : (int?)null;
            //empInfo.FirstName = txtFirstName.Text;
            //empInfo.LastName = txtLastName.Text;
            //empInfo.FirstNameEng = txtFirstNameEng.Text;
            //empInfo.LastNameEng = txtLastNameEng.Text;
            //empInfo.GenderSeq = ddlGenderSeq.SelectedValue.Length > 0 ? Int32.Parse(ddlGenderSeq.SelectedValue) : (int?)null;
            //empInfo.RaceNatSeq = ddlRaceNatSeq.SelectedValue.Length > 0 ? Int32.Parse(ddlRaceNatSeq.SelectedValue) : (int?)null;
            //empInfo.NatSeq = ddlNatSeq.SelectedValue.Length > 0 ? Int32.Parse(ddlNatSeq.SelectedValue) : (int?)null;
            //empInfo.ReligionSeq = ddlReligionSeq.SelectedValue.Length > 0 ? Int32.Parse(ddlReligionSeq.SelectedValue) : (int?)null;
            //empInfo.BloodGroup = txtBloodGroup.Text;
            //empInfo.BirthDate = txtBirthDate.Text.Length > 0 ? DateTime.Parse(txtBirthDate.TextWithLiterals.Trim()) : (DateTime?)null;
            //empInfo.Citizen = txtCitizen.Text;
            //empInfo.TaxPayer = txtTaxPayer.Text;
            //empInfo.Social = txtSocial.Text;
            //empInfo.DeathDate = txtDeathDate.Text.Length > 0 ? DateTime.Parse(txtDeathDate.TextWithLiterals.Trim()) : (DateTime?)null;
            //empInfo.UpdatedBy = 1;

            //emp.UpdatePersonal(empInfo);

            MessageX.Alert(Page, "บันทึกรายการเรียบร้อยแล้ว!!");
        }

        protected void ddlGenderSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDDLNamePrefixSeq();

            if (ddlNamePrefixSeq.Items.Count > 0)
            {
                ddlNamePrefixSeq.SelectedIndex = 0;
                ddlNamePrefixSeq.Text = "";
            }
        }
    }
}